<div id="cache-runs">
  <.card title={gettext("Cache Runs")} icon="schema" data-part="cache-runs">
    <.card_section data-part="cache-runs-section">
      <div data-part="filters">
        <.dropdown
          id="cache-runs-sort-by"
          label={
            case @cache_runs_sort_by do
              "duration" -> gettext("Duration")
              "hit_rate" -> gettext("Hit rate")
              _ -> gettext("Ran at")
            end
          }
          secondary_text={gettext("Sort by:")}
        >
          <.dropdown_item
            value="hit_rate"
            label={gettext("Hit rate")}
            patch={cache_runs_dropdown_item_patch_sort("hit_rate", @uri)}
            data-selected={@cache_runs_sort_by == "hit_rate"}
          >
            <:right_icon><.check /></:right_icon>
          </.dropdown_item>
          <.dropdown_item
            value="duration"
            label={gettext("Duration")}
            patch={cache_runs_dropdown_item_patch_sort("duration", @uri)}
            data-selected={@cache_runs_sort_by == "duration"}
          >
            <:right_icon><.check /></:right_icon>
          </.dropdown_item>
          <.dropdown_item
            value="ran_at"
            label={gettext("Ran at")}
            patch={cache_runs_dropdown_item_patch_sort("ran_at", @uri)}
            data-selected={@cache_runs_sort_by == "ran_at"}
          >
            <:right_icon><.check /></:right_icon>
          </.dropdown_item>
        </.dropdown>
        <.filter_dropdown
          id="filter-dropdown"
          label={gettext("Filter")}
          available_filters={@available_filters}
          active_filters={@active_filters}
        />
      </div>
      <div :if={Enum.any?(@active_filters)} data-part="active-filters">
        <.active_filter :for={filter <- @active_filters} filter={filter} />
      </div>

      <div :if={not Enum.empty?(@cache_runs)} data-part="cache-runs-table">
        <.table
          id="cache-runs-table"
          rows={@cache_runs}
          row_navigate={
            fn cache_run ->
              url(
                ~p"/#{@selected_project.account.name}/#{@selected_project.name}/runs/#{cache_run.id}"
              )
            end
          }
        >
          <:col :let={cache_run} label={gettext("Command")}>
            <.text_and_description_cell label={"tuist " <> (if cache_run.command_arguments, do: (cache_run.command_arguments |> String.split(" ") |> Enum.take(2) |> Enum.join(" ")), else: "")} />
          </:col>
          <:col
            :let={cache_run}
            label={gettext("Hit rate")}
            patch={@cache_runs_sort_by == "hit_rate" && column_patch_sort(assigns, "hit_rate")}
            icon={
              @cache_runs_sort_by == "hit_rate" &&
                sort_icon(@cache_runs_sort_order)
            }
          >
            <.text_cell label={
              gettext("%{hit_rate}%",
                hit_rate:
                  if is_nil(cache_run.hit_rate) do
                    0
                  else
                    cache_run.hit_rate
                    |> Decimal.from_float()
                    |> Decimal.round(1)
                  end
              )
            } />
          </:col>
          <:col :let={cache_run} label="Status">
            <%= if cache_run.status == :success do %>
              <.status_badge_cell label={gettext("Passed")} status="success" />
            <% else %>
              <.status_badge_cell label={gettext("Failed")} status="error" />
            <% end %>
          </:col>
          <:col :let={cache_run} label={gettext("Branch")}>
            <.text_cell icon="git_branch" label={cache_run.git_branch || "None"} />
          </:col>
          <:col :let={cache_run} label={gettext("Ran by")}>
            <.run_ran_by_badge_cell run={cache_run} ran_by_name={cache_run.user_account_name} />
          </:col>
          <:col
            :let={cache_run}
            label={gettext("Duration")}
            patch={@cache_runs_sort_by == "duration" && column_patch_sort(assigns, "duration")}
            icon={
              @cache_runs_sort_by == "duration" &&
                sort_icon(@cache_runs_sort_order)
            }
          >
            <.text_cell
              label={
                gettext("%{run_duration}s",
                  run_duration:
                    (cache_run.duration / 1000) |> Decimal.from_float() |> Decimal.round(2)
                )
              }
              icon="history"
            />
          </:col>
          <:col
            :let={cache_run}
            label={gettext("Ran at")}
            patch={@cache_runs_sort_by == "ran_at" && column_patch_sort(assigns, "ran_at")}
            icon={
              @cache_runs_sort_by == "ran_at" &&
                sort_icon(@cache_runs_sort_order)
            }
          >
            <.text_cell sublabel={Tuist.Utilities.DateFormatter.from_now(cache_run.ran_at)} />
          </:col>
        </.table>
        <.pagination
          uri={@uri}
          has_previous_page={@cache_runs_meta.has_previous_page?}
          has_next_page={@cache_runs_meta.has_next_page?}
          start_cursor={@cache_runs_meta.start_cursor}
          end_cursor={@cache_runs_meta.end_cursor}
        />
      </div>

      <div :if={Enum.empty?(@cache_runs)} data-part="empty-cache-runs">
        <.empty_card_section
          title={gettext("No data yet")}
          get_started_href="https://docs.tuist.dev/en/guides/develop/cache"
          data-part="empty-cache-runs-table-card-section"
        >
          <:image>
            <img src="/images/empty_table_light.png" data-theme="light" />
            <img src="/images/empty_table_dark.png" data-theme="dark" />
          </:image>
        </.empty_card_section>
      </div>
    </.card_section>
  </.card>
</div>
